<?php
namespace app\controller;

use app\BaseController;
use app\Common;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use think\facade\Session;
use think\facade\Cache;
use think\response\Json;

use app\model\UserModel;
use app\model\DeptModel;

//use app\Common;

class Admin extends BaseController
{
    //常用数据表内容 及常用字典
    var $goodsType=[];
    var $dept=[];
    var $users =[];
    var $status=array('1'=>'正常','2'=>'无效');


    public function __construct(){

        if(!Session::get('logonid')) header("location:/index.php/login");
        //准备一个类型数组
        $list = Db::table("cat")->select()->toArray();
        $this->goodsType['0'] = '';
        foreach($list as $k=>$v){
            $this->goodsType[$v['id']]=$v['name'];
        }

        $um=new UserModel();
        $this->users = $um->getAll();
        
        $dm=new DeptModel();
        $this->dept = $dm->getAll();

        $sys=Db::table('sys')->find(1);
        $tdk=  explode("----",$sys['tdk']);
        $sys['tdk1']=!empty($tdk[0])?$tdk[0]:'导航首页';
        $sys['tdk2']=!empty($tdk[0])?$tdk[1]:'网站导航';
        $sys['tdk3']=!empty($tdk[0])?$tdk[2]:'网站,导航,信息,软件';
        $this->sys=$sys;
        unset($sys);

    }

    //后台管理入口
    public function index(){
        $uid=Session::get('logonid');
        if(empty($uid)) return header("location:/login/index");

        $sys = $this->sys;
        Session::set('sys',$sys);

        $user = Db::table('user')->find($uid);

        //system munu
        $menu1 = Db::query("SELECT * FROM `menu` where status=1 order by sort");

        //菜单处理,子菜单排序
        $menuDo=[];
        foreach($menu1 as $kv){

            if($kv['fid']==0){
                $g_list = explode(",",$kv['group_id']);
                 if($user['group_id']>1 && !in_array($user['group_id'],$g_list)) continue;

                $kv['class'] = "top_menu";
                $menuDo[] =$kv;
                
                foreach($menu1 as $kv1){
                    $g_list = explode(",",$kv1['group_id']);
                    if($user['group_id']>1 && !in_array($user['group_id'],$g_list)) continue;

                    if($kv1['fid']==$kv['id']) {
                        $kv1['class']= "second_menu fid_".$kv['id'];
                        $menuDo[]=$kv1;
                    }
                }
            }
        }

        $dept = Db::table('dept')->where('id',$user['dept_id'])->find();

        View::assign([
            "sys"=>$sys,
            "menu"=>$menuDo,
            'user'=>$user,
            'dept'=>$dept
        ]);

        return View::fetch();

    }


    //购物车
    public function cart(){
        $list = Db::table("cart")->where('status',1)->select()->toArray();
        return json($list);
    }

    //订单
    public function order(){
        $list = Db::table("customer")->where('status',1)->select()->toArray();
        return json($list);
    }   

    //菜单管理
    public function menu(){
        $list = Db::table("menu")->where('status',1)->select();
        return json($list);
    }

    //用户管理
    public function user(){
        $list = Db::table("user")->where('status',1)->select();
        return json($list);
    }    

    //我的资料
    public function me(){
        $uid = Session::get('uid');
        $list = Db::table("user")->where('uid',$uid)->select();
        return json($list);
    } 

    //保持session
    public function ajax_breath(){
        $data=[];
        $data['uid'] = Session::get('logonid');
        if(empty($data['uid'])) {
            return json(['code'=>0,'msg'=>'重新登录']);
        }
        return json(['code'=>1,'data'=>$data]);
    }


    //system infomation
    public function sys(){
        $uid = Session::get('logonid');
        $all=Request::param();
        if(empty($uid)) return header("location:/login/index/target/admin");

        if(!empty($all['sys_name'])){
            $res = Db::table('sys')->update($all);
            //$list = Db::table("sys")->find(1);
        }

        $list = Db::table("sys")->find(1);

        if(!empty($all['app'])) {
            return json(['code'=>1,'data'=>$list]);
        }

        View::assign(['sys'=>$list]);
        return View::fetch('sys');
    }     

    public function home(){
        // $all=Request::param();
        // if(!empty($all['app'])){
        //     $sys=Db::table('sys')->find(1);
        //     $sys['pwd_msg'] = Session::get('pwd_msg');
            
        //     return json(['code'=>1,'sys'=>$sys]);
        // }
        return header("location:/goods/index");
        return View::fetch('home');
    }


    public function img_list(){
        $dir = Request::param('upload_dir');
        if(!$dir) {
            $dir='upload';
        }

        Common::img_list($dir);
    }
    
    public function db_init(){
       $uid=Session::get('logonid');
       if($uid!=1) return "没有权限！";
       return common::db_init();
    }
   
}
